﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace olierFrm.Extensions.EString
{
    public static class StringExtensions
    {
        public static string Slice(this string s, int start, int end)
        {
            if (end < 0)
            {
                end = s.Length + end;
            }
            int len = end - start;
            return s.Substring(start, len);
        }
        public static string ToStringSplit(this List<string> lis)
        {
            if (lis == null || lis.Count == 0) return "";
            string rLis = "";
            foreach (var s in lis)
            {
                rLis += s + ";";
            }
            return rLis.Substring(0, rLis.Length - 1);
        }

        /// <summary>
        /// 日期转换 yyyy-MM-dd hh:mm:ss
        /// </summary>
        /// <param name="s"></param>
        /// <returns>yyyy-MM-dd HH:mm:ss</returns>
        public static string ToLongDateTime(this object s)
        {
            if (s == null || s.ToString().Length == 0) return "";

            DateTime dt;
            if (DateTime.TryParse(s.ToString(), out dt))
            {
                return DateTime.Parse(s.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            }
            return "";
        }

        /// <summary>
        /// 日期转换 yyyy-MM-dd
        /// </summary>
        /// <param name="s"></param>
        /// <returns>yyyy-MM-dd</returns>
        public static string ToShortDate(this object s)
        {
            if (s == null || s.ToString().Length == 0) return "";

            DateTime dt;
            if (DateTime.TryParse(s.ToString(), out dt))
            {
                return DateTime.Parse(s.ToString()).ToString("yyyy-MM-dd");
            }
            return "";
        }

        /// <summary>
        /// 日期转换 yyyy-MM-dd
        /// </summary>
        /// <param name="s"></param>
        /// <returns>yyyy-MM-dd</returns>
        public static string ToShortTime(this object s)
        {
            if (s == null || s.ToString().Length == 0) return "";

            DateTime dt;
            if (DateTime.TryParse(s.ToString(), out dt))
            {
                return DateTime.Parse(s.ToString()).ToString("hh:mm:ss");
            }
            return "";
        }
    }
}
